package com.demoworld.service;

/**
 * Created by francis on 2017/6/14.
 */
public abstract class AbstractCommonSystemService implements Runnable {
    private Thread mainWork;
    /**
     * 服务是否已经停止
     */
    private boolean isServiceStoped;
    /**
     * 当前服务名称
     */
    private String serviceName;
    /**
     * 服务功能描述
     */
    private String serviceDescription;
    /**
     * 是否作为服务一直运行
     * true： 一直运行服务， 不需要停止
     * false： 值执行一次服务方法
     */
    private boolean isAlwaysRunning;
    /**
     * 记录上次错误信息
     */
    private Exception lastException;

    /**
     * 初始化 系统服务
     *
     * @param runningAlways  是否一致运行该服务
     * @param theServiceName 服务名称
     */
    public AbstractCommonSystemService(boolean runningAlways, String theServiceName) {
        isAlwaysRunning = runningAlways;
        serviceName = theServiceName;
        System.out.println("AbstractCommonSystemService init isAlwaysRunning=" + this.isAlwaysRunning + ",isServiceStoped=" + this.isServiceStoped);
        this.isServiceStoped = true;
        System.out.println("AbstractCommonSystemService init isAlwaysRunning=" + this.isAlwaysRunning + ",isServiceStoped=" + this.isServiceStoped);
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getServiceDescription() {
        return serviceDescription;
    }

    public void setServiceDescription(String serviceDescription) {
        this.serviceDescription = serviceDescription;
    }

    /**
     * process the Test logic
     * must be overrided
     */
    public abstract void process();

    /**
     * 重写 Runnable run方法
     */
    @Override
    public void run() {
        do {
            try {
                process();
            } catch (Exception ex) {
                System.out.println(String.format("%s-process().exceprion", this.getServiceName()));
                ex.printStackTrace();
            }
        }
        while (this.isAlwaysRunning && !this.isServiceStoped);
        System.out.println(String.format("%s is stopped.", this.getServiceName()));
    }

    /**
     * start the service
     */
    public synchronized void start() {
        System.out.println(String.format("%s is starting...", this.getServiceName()));
        if (mainWork != null && mainWork.isAlive()) {
            return;
        }
        isServiceStoped = false;
        mainWork = new Thread(this);
        mainWork.setDaemon(false);
        mainWork.start();
    }

    /**
     * stop the service
     */
    public synchronized void stop() {
        System.out.println(String.format("%s is stopping...", this.getServiceName()));
        isServiceStoped = true;
    }

    /**
     * 获取上次异常对象
     *
     * @return
     */
    public Exception getLastException() {
        return this.lastException;
    }

    public String getServiceStatus() {
        return String.format("isServiceStoped=%s, thread isalive=%s", isServiceStoped, mainWork.isAlive());
    }
}